package com.three.iot.service.core;

import com.three.iot.commons.dto.UserModiPassDto;
import com.three.iot.commons.enums.IdTypeEnum;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.commons.utils.IDUtils;
import com.three.iot.commons.utils.MD5Util;
import com.three.iot.jdbc.mapper.SysUserMapper;
import com.three.iot.jdbc.model.SysUser;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author luiz
 * @Title: LoginCoreService
 * @ProjectName iot
 * @Description: TODO
 * @date 2019-04-04 15:19
 */
@Service
public class LoginCoreService {
    private static final Logger logger = LoggerFactory.getLogger(LoginCoreService.class);

    @Resource
    private SysUserMapper sysUserMapper;

    @Value("three.pass.salt:123456!@#123QweAsd")
    private String threePassSalt;

    // @Transactional


    public Result<String> resetPass(String userNo) {
        SysUser sysUser = sysUserMapper.selectByUserNo(userNo);
        if (sysUser == null) {
            return Result.newError(ResultCode.USER_NOT_EXISTS);
        }
        String initPass = RandomStringUtils.randomAlphanumeric(8);
        logger.info("账号:{}的密码已经被重置为:{}", userNo, initPass);
        sysUser.setLoginPass(MD5Util.genPass(initPass, threePassSalt));
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return Result.newSuccess();
    }


    public Result forgetPass(UserModiPassDto userModiPassDto) {
        //生成链接，发送到用户邮箱 todo
        String randomStr = IDUtils.genIdStr(IdTypeEnum.USER.getCode());
        SysUser sysUser = sysUserMapper.selectByUserNo(userModiPassDto.getLoginNo());
        if (sysUser == null) {
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        String dbPass = sysUser.getLoginPass();
        String reqPass = MD5Util.genPass(userModiPassDto.getOldPass(), threePassSalt);
        if (!reqPass.equals(dbPass)) {
            logger.error("密码错误！不能修改操作!");
            return Result.newError(ResultCode.USER_OLD_PASS_ERROR);
        }
        SysUser tkUser = new SysUser();
        tkUser.setUserNo(userModiPassDto.getLoginNo());
        tkUser.setResv1(randomStr);
        sysUserMapper.updateByUserNo(tkUser);

        //发送邮件 todo
        return null;
    }


    public Result modiPass(UserModiPassDto userModiPassDto) {
        //用户拿到链接后 进行密码修改
        SysUser oldTkUser = sysUserMapper.selectByUserNo(userModiPassDto.getLoginNo());
        if (oldTkUser == null) {
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        if (!oldTkUser.getResv1().equals(userModiPassDto.getForgetRadomStr())) {
            return Result.newError(ResultCode.COMMON_UNVALID_URL);
        }
        SysUser tkUser = new SysUser();
        tkUser.setUserNo(oldTkUser.getUserNo());
        tkUser.setLoginPass(MD5Util.genPass(userModiPassDto.getPasswordNew1(), threePassSalt));
        sysUserMapper.updateByPrimaryKeySelective(tkUser);
        return Result.newSuccess();
    }
}
